/* 
 * Copyright (C) 2006-2011 Crise, crise<at>mail.berlios.de
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef HTTP_MANAGER_H
#define HTTP_MANAGER_H

#include <map>

#include <boost/function.hpp>
#include <boost/bind.hpp>

#include "forward.h"
#include "typedefs.h"

#include "Singleton.h"
#include "TimerManager.h"
#include "Util.h"

namespace dcpp {

using std::map;

class HttpManager : public Singleton<HttpManager>, public TimerManagerListener
{
public:
	typedef boost::function<void (const HttpConnection*, const string&, uint8_t)> CallBack;

	enum Status {
		HTTP_COMPLETE = 0x01,
		HTTP_FAILED = 0x02,
		HTTP_ABORT = 0x04,
		HTTP_CORAL = 0x08
	};

	void addDownload(const string& aUrl, const CallBack& cbFunc, bool allowCache = true, const string& aUserAgent = Util::emptyString);
	void addFileDownload(const string& aUrl, const string& aFile, const CallBack& cbFunc, bool allowCache = true, const string& aUserAgent = Util::emptyString);

	void postForm(const string& aHandler, const StringMap& aData, const CallBack& cbFunc, const string& aUserAgent = Util::emptyString);

private:
	friend class Singleton<HttpManager>;
	friend class HttpDownload;

	HttpManager() { TimerManager::getInstance()->addListener(this); };
	~HttpManager();

	void putDownload(HttpDownload* dl) noexcept;

	// TimerManagerListener
	void on(TimerManagerListener::Second, uint64_t /*aTick*/) noexcept;

	CriticalSection cs;
	map<string,  HttpDownload*> active;
	vector<HttpDownload*> finished;
};

}; // namespace dcpp

#endif // HTTP_MANAGER_H
